# -*- coding: utf-8 -*-

from w.blocks.containers.block import Block as BlockContainer, IBlock

from w.html.doctypes.preferable import Div
from w.javascript.expression.translator import Link
from w.javascript.expression import JS, toJS

from w.settings import W

from w.html.useful import JScript

class Error(Exception):
    pass

class BaseBlock(object):
    scriptFiles = ['w/thirdparty/prototype_extract.js',
                   'w/thirdparty/json.js',
                   'w/thirdparty/dhtmlHistory.js',
                   'w/blocks/ww.js',
                   'w/blocks/block.js']
    serverDownText = 'Server is busy... Try again later, please.'
    
    jsw = Link('window')#TODO:move smwhr
    jsd = Link('document')#TODO:move smwhr
    
    noJSInit = False

    def __init__(self, *argv, **kwargv):
        if len(argv):
            raise Error('Only named params allowed')

        self.js = Link(self.blockID)
        self.jsElement = Link(self.jsd.getElementById(self.blockID))

        self.wrapper = Div(hid=self.blockID)

    #holy shit, isn't it? It's an example of that you MUST use javascript.expression only for 
    #expressions or VERY simple statements.
    def jsCode(self):
        return JS.var(self.js) << JS.new(Link(self.className)(self.blockID,self.serverDownText))

    def make(self, content=[]):
        if(self.wrapper is not None):
            self.wrapper.extend(content)
            return [self.wrapper]
        return content

class Block(BlockContainer, BaseBlock):
    def __init__(self, content='', document=None, *argv, **kwargv):
        BlockContainer.__init__(self, content, *argv, **kwargv)
        BaseBlock.__init__(self, document=document, *argv, **kwargv)

    def make(self, content=[]):
        return BaseBlock.make(self, BlockContainer.make(self, content))

